package com.fushenlan.commons;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther: JIAJIA
 * @date: 2018/8/20  11:24
 * @description:
 */
public class ListPagingUtils <T> {
    private static final long serialVersionUID = 1L;
    // 总记录数
    private int totalCount;
    // 每页记录数
    private int pageSize;
    // 总页数
    private int totalPage;
    // 当前页数
    private int currPage;
    // 列表数据
    private List<?> list;
    public ListPagingUtils(List<?> list, int pageSize, int currPage) {
        this.list = listPaging(list,currPage,pageSize);
        this.totalCount = list.size();
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }
    public ListPagingUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = listPaging(list,currPage,pageSize);
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    /**@Auther: Sunny
     * @param list 进行分页的list
     * @param pageNo 页码
     * @param pageSize 每页显示条数
     * @return 分页后数据
     */
    public  <T> List<T> listPaging(List<T> list, Integer pageNo, Integer pageSize){

        if(list == null){
            list = new ArrayList<T>();
        }

        int totalitems = list.size();

        List<T> pagingList = new ArrayList<T>();

       
        int totalNum = ((pageNo - 1) * pageSize) + pageSize > totalitems ? totalitems : ((pageNo - 1) * pageSize) + pageSize;
        for(int i = (pageNo-1)*pageSize; i < totalNum; i++) {
            pagingList.add(list.get(i));
        }
        this.currPage=pageNo;
        this.pageSize=pageSize;
        this.totalCount=list.size();
        this.totalPage=list.size()%pageSize==0?list.size()/pageSize:list.size()/pageSize+1;
        return pagingList;
    }

}
